﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HiTeachers.Web.Controls
{
    [ToolboxData("<{0}:ResizedImage runat=server></{0}:ResizedImage>")]
    public class ResizedImage : Image
    {
        public int MaxWidth
        {
            get { return (ViewState["MaxWidth"] == null) ? 50 : (int)ViewState["MaxWidth"]; }
            set { ViewState["MaxWidth"] = value; }
        }

        public int MaxHeight
        {
            get { return (ViewState["MaxHeight"] == null) ? 50 : (int)ViewState["MaxHeight"]; }
            set { ViewState["MaxHeight"] = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            GenerateEmptyAlternateText = true;


            ImageUrl = string.Format(
                "~/ImageScale.ashx?image={0}&height={1}&width={2}",
                HttpUtility.UrlEncode(ImageUrl),
                MaxHeight,
                MaxWidth
            );
        }
    }
}
